package com.example.myapplication.tack_05;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.tack_05.data.PersonData;
import com.example.myapplication.tack_05.data.SetPersonData;
import com.example.myapplication.utils.OkHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

public class Person extends AppCompatActivity {
    private ImageView exit;
    private JSONObject jsonObject;
    private LinearLayout save;
    private EditText username;
    private EditText phone;
    private RadioGroup radio_group;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private RadioButton radioButton1;
    private RadioButton radiofButton2;
    private PersonData personData;
    private String jsonName;
    private String jsonPhe;
    private String jsonSex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_05_person);
        init();
        exit_();
        //查看个人全部信息
        view_();
        clickSex();
        //修改个人信息
        clickSave();


    }

    private void view_() {
        String url = "prod-api/api/common/user/getInfo";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, PersonData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                personData = (PersonData) obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        editor.putString("username",personData.getUser().getUserName());
                        editor.putString("phonenumber",personData.getUser().getPhonenumber());
                        editor.putString("sex",personData.getUser().getSex());
                        editor.apply();
                        String name = sharedPreferences.getString("username", "");
                        String phe = sharedPreferences.getString("phonenumber", "");
                        String sex = sharedPreferences.getString("sex", "");
                        username.setText(name);
                        phone.setText(phe);
                        if("1".equals(sex)){
                            radioButton1.setChecked(true);
                        }else if("0".equals(sex)){
                            radiofButton2.setChecked(true);
                        }
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }
    private void clickSex(){
        jsonSex= "";
        radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(R.id.radio_button_1 == group.getCheckedRadioButtonId()) {
                    jsonSex = "1";
                }else if(group.getCheckedRadioButtonId() ==  R.id.radio_button_2){
                    jsonSex = "0";
                }
            }
        });
    }
    private void clickSave() {
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jsonName = username.getText().toString();
                jsonPhe = phone.getText().toString();

                try {
                    jsonObject.put("nickName", jsonName);
                    jsonObject.put("phonenumber", jsonPhe);
                    jsonObject.put("sex", jsonSex);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = jsonObject.toString();
                runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       SetPerSon(json,jsonName,jsonPhe,jsonSex);
                   }
               });
            }
        });
    }

    private void SetPerSon(String json, String jsonName, String jsonPhe, String jsonSex) {
        String url = "prod-api/api/common/user";
        OkHttpTool.doNetRequest(url, OkHttpTool.PUT, SetPersonData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                SetPersonData setPersonData = (SetPersonData) obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        PersonAdapter(setPersonData,jsonName,jsonPhe,jsonSex);

                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    private void PersonAdapter(SetPersonData setPersonData, String jsonName, String jsonPhe, String jsonSex) {
        if(setPersonData.getCode() == 200){
            Toast.makeText(Person.this,setPersonData.getMsg(),Toast.LENGTH_SHORT).show();
            Person.this.finish();
        }else{
            Toast.makeText(Person.this,setPersonData.getMsg(),Toast.LENGTH_SHORT).show();
        }
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Person.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        jsonObject = new JSONObject();
        save = findViewById(R.id.save);
        username = findViewById(R.id.username);
        phone = findViewById(R.id.phone);
        radio_group = findViewById(R.id.radio_group);
        radioButton1 = findViewById(R.id.radio_button_1);
        radiofButton2 = findViewById(R.id.radio_button_2);
        sharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();


    }
}